package com.diver.flinkdemo.service.impl;/**
 * @author yangs
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diver.flinkdemo.dto.xxt.C40DataVO;
import com.diver.flinkdemo.entity.xxt.DeviceMode;
import com.diver.flinkdemo.entity.xxt.LocationMode;
import com.diver.flinkdemo.entity.xxt.MoreSetting;
import com.diver.flinkdemo.mapper.mysqlMapper.MoreSettingMapper;
import com.diver.flinkdemo.service.IClockSettingsService;
import com.diver.flinkdemo.service.IDeviceModeService;
import com.diver.flinkdemo.service.IMoreSettingService;
import com.diver.flinkdemo.service.IRedisService;
import com.diver.flinkdemo.utils.CommonResource;
import com.diver.flinkdemo.utils.DateUtil;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import com.diver.flinkdemo.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yangs
 */
//@Service
@Slf4j
public class MoreSettingService implements IMoreSettingService {
    Logger logger = LoggerFactory.getLogger(getClass());

//    @Autowired
//    private KafkaClient kafkaClient;
//
//    @Autowired
//    private TrajectoryClient trajectoryClient;
//    @Autowired
    private IRedisService redisService = new RedisServiceImpl();
//
//    @Autowired
//    private DeviceModeMapper deviceModeMapper;
//
//    @Autowired
    private MoreSettingMapper mapper;
    private SqlSession session;
//
//    @Autowired
    private IDeviceModeService deviceModeService = new DeviceModeServiceImpl();
//
//    @Autowired
    private IClockSettingsService clockSettingsService = new ClockSettingsServiceImpl();

    @Override
    public MoreSetting getSettingByImei(String imei) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(MoreSettingMapper.class);
            return mapper.selectOne(new QueryWrapper<MoreSetting>().eq("imei", imei));
        } finally {
            endOfSession(session);
        }
    }

//    /**
//     * 通话音量
//     * @param imei
//     * @param level 2高，1中，0低
//     * @param cardType
//     * @return
//     */
//    @Override
//    public boolean setPhoneLevel(String imei, Integer level, String cardType){
//        MoreSetting ms = getSetting(imei);
//        ms.setPhoneLevel(level);
//        if(this.saveOrUpdateSetting(ms)){
//            JSONObject jo = new JSONObject();
//            jo.put("imei", imei);
//            if (cardType.equals(StuCardTypeEnum.XTC0.getType())) {
//                jo.put("type", "PHONELEVEL");
//                jo.put("level", level);
//                kafkaClient.tcpApplicationSend(jo.toJSONString());
//            }
//            if (cardType.equals(StuCardTypeEnum.XTC40.getType()) || cardType.equals(StuCardTypeEnum.XTC2.getType())) {
////                Integer voice = ms.getSpeakerLevel();
////                Integer ring = ms.getAlarmClockLevel();
//                jo.put("type", "SPEAKER1");
//                jo.put("call", level == 2 ? 0 : (level == 0 ? 2 : level));//0，1，2-->大中小
//                jo.put("voice", "");//语音播报
//                jo.put("ring", "");//响铃
//                kafkaClient.dalApplicationSend(jo.toJSONString());
//            } else if(cardType.equals(StuCardTypeEnum.XTC1.getType())) {
//                Integer voice = ms.getSpeakerLevel();
//                Integer ring = ms.getAlarmClockLevel();
//                jo.put("type", "SPEAKER1");
//                jo.put("call", level == 2 ? 0 : (level == 0 ? 2 : level));//0，1，2-->大中小
//                jo.put("voice", voice == 2 ? 0 : (voice == 0 ? 2 : voice));//语音播报
//                jo.put("ring", ring == 2 ? 0 : (ring == 0 ? 2 : ring));//响铃
//                kafkaClient.dalApplicationSend(jo.toJSONString());
//            }
//            return true;
//        }
//        return false;
//    }
//
//    /**
//     * 播报音量
//     * @param imei
//     * @param level 2高，1中，0低
//     * @param cardType
//     * @return
//     */
//    @Override
//    public boolean setSpeakerLevel(String imei, Integer level, String cardType){
//        MoreSetting ms = getSetting(imei);
//        ms.setSpeakerLevel(level);
//        if(this.saveOrUpdateSetting(ms)){
//            JSONObject jo = new JSONObject();
//            jo.put("imei", imei);
//            if (cardType.equals(StuCardTypeEnum.XTC0.getType())) {
//                jo.put("type", "SPEAKER");
//                jo.put("level", level);
//                kafkaClient.tcpApplicationSend(jo.toJSONString());
//            }
//            if (cardType.equals(StuCardTypeEnum.XTC40.getType()) || cardType.equals(StuCardTypeEnum.XTC2.getType())) {
////                Integer call = ms.getPhoneLevel();
////                Integer ring = ms.getAlarmClockLevel();
//                jo.put("type", "SPEAKER1");
//                jo.put("call", "");//通话音量0，1，2-->大中小
//                jo.put("voice", level == 2 ? 0 : (level == 0 ? 2 : level));//语音播报
//                jo.put("ring", "");//响铃
//                kafkaClient.dalApplicationSend(jo.toJSONString());
//            } else if(cardType.equals(StuCardTypeEnum.XTC1.getType())) {
//                Integer call = ms.getPhoneLevel();
//                Integer ring = ms.getAlarmClockLevel();
//                jo.put("type", "SPEAKER1");
//                jo.put("call", call == 2 ? 0 : (call == 0 ? 2 : call));//通话音量0，1，2-->大中小
//                jo.put("voice", level == 2 ? 0 : (level == 0 ? 2 : level));//语音播报
//                jo.put("ring", ring == 2 ? 0 : (ring == 0 ? 2 : ring));//响铃
//                kafkaClient.dalApplicationSend(jo.toJSONString());
//            }
//            return true;
//        }
//        return false;
//    }
//
//    /**
//     * 闹铃
//     * @param imei
//     * @param level 2高，1中，0低
//     * @param cardType
//     * @return
//     */
//    @Override
//    public boolean setAlarmClockLevel(String imei, Integer level, String cardType){
//        MoreSetting ms = getSetting(imei);
//        ms.setAlarmClockLevel(level);
//        if(this.saveOrUpdateSetting(ms)){
//            JSONObject jo = new JSONObject();
//            jo.put("imei", imei);
//            if (cardType.equals(StuCardTypeEnum.XTC0.getType())) {
//                jo.put("type", "SPEAKER");
//                jo.put("level", level);
//                kafkaClient.tcpApplicationSend(jo.toJSONString());
//            }
//            if (cardType.equals(StuCardTypeEnum.XTC40.getType()) || cardType.equals(StuCardTypeEnum.XTC2.getType())) {
////                Integer call = ms.getPhoneLevel();
////                Integer voice = ms.getSpeakerLevel();
//                jo.put("type", "SPEAKER1");
//                jo.put("call", "");//通话音量0，1，2-->大中小
//                jo.put("voice", "");//语音播报
//                jo.put("ring", level == 2 ? 0 : (level == 0 ? 2 : level));//响铃
//                kafkaClient.dalApplicationSend(jo.toJSONString());
//            } if(cardType.equals(StuCardTypeEnum.XTC1.getType())) {
//                Integer call = ms.getPhoneLevel();
//                Integer voice = ms.getSpeakerLevel();
//                jo.put("type", "SPEAKER1");
//                jo.put("call", call == 2 ? 0 : (call == 0 ? 2 : call));//通话音量0，1，2-->大中小
//                jo.put("voice", voice == 2 ? 0 : (voice == 0 ? 2 : voice));//语音播报
//                jo.put("ring", level == 2 ? 0 : (level == 0 ? 2 : level));//响铃
//                kafkaClient.dalApplicationSend(jo.toJSONString());
//            }
//            return true;
//        }
//        return false;
//    }

    @Override
    public boolean setModifyMode(String imei, String modeId, String cardType){
        MoreSetting ms = getSetting(imei);
        if(StringUtils.isNotBlank(modeId) && modeId.equals(ms.getModeId())) {
            return true;
        }
        ms.setModeId(modeId);
        if (this.saveOrUpdateSetting(ms)) {
            /*if (!CommonResource.MODE_TYPE_INTELLIGENT.equals(modeType)) {//非智能模式，立即下发指令
                DeviceMode mode = deviceModeService.getById(modeType);
                if (null != mode) {
                    log.info("{} {}模式处理：{}", modeType, mode.getModeName(), imei);
                    kafkaClient.sendMod(imei, cardType, mode.getGpsRepInterval());
                }
            } else {
                log.info("{} {}模式处理：{}", modeType, "默认智能模式", imei);
                kafkaClient.sendMod(imei, cardType, 600);
            }*/
            DeviceMode mode = deviceModeService.getById(modeId);
            if (null != mode) {
                LocationMode locationMode = new LocationMode();
                locationMode.setGpsRepInterval(mode.getGpsRepInterval());
                locationMode.setPositionFirst(mode.getPositionFirst());
                log.info("{} {}模式处理：{}", modeId, mode.getModeName(), imei);
                //暂时注释
//                kafkaClient.sendMod(imei, cardType, locationMode);
            }
            /*
            JSONObject jo = new JSONObject();
            jo.put("imei", imei);
            if (cardType.equals(StuCardTypeEnum.XTC0.getType())) {
                jo.put("type", "MODE");
                jo.put("mode", modeType);
                kafkaClient.tcpApplicationSend(jo.toJSONString());
            }
            if (cardType.equals(StuCardTypeEnum.XTC1.getType())) {
                String time = "300";
                switch (modeType) {
                    case "m1"://定位模式 5分钟
                        time = "300";
                        break;
                    case "m2"://省电模式 30分钟
                        time = "1800";
                        break;
                    case "m3"://休眠模式 不上报
                        time = "0";
                        break;
                }
                jo.put("type", "UPMODE");
                jo.put("mode", "m1");
                jo.put("time", time);
                kafkaClient.dalApplicationSend(jo.toJSONString());
            }*/
            return true;
        }
        return false;
    }

//    @Override
//    public boolean setSosStatus(String imei, Integer sosStatus){
//        MoreSetting ms = getSetting(imei);
//        ms.setSosStatus(sosStatus);
//        if(this.saveOrUpdateSetting(ms)){
//            JSONObject jo = new JSONObject();
//            jo.put("imei", imei);
//            jo.put("type", "SOSFUN");
//            jo.put("flag", sosStatus);
//            kafkaClient.tcpApplicationSend(jo.toJSONString());
//            return true;
//        }
//        return false;
//    }
//
//    @Override
//    public boolean monitor(String imei, String phone, String cardType) {
//        JSONObject jo = new JSONObject();
//        jo.put("imei", imei);
//        jo.put("type", "MONITOR");
//        jo.put("phone", phone);
//        if (cardType.equals(StuCardTypeEnum.XTC0.getType())) {
//            kafkaClient.tcpApplicationSend(jo.toJSONString());
//        }
//        if (cardType.equals(StuCardTypeEnum.XTC1.getType()) || cardType.equals(StuCardTypeEnum.XTC40.getType()) || cardType.equals(StuCardTypeEnum.XTC2.getType())) {
//            kafkaClient.dalApplicationSend(jo.toJSONString());
//        }
//        return true;
//    }

    private boolean saveOrUpdateSetting(MoreSetting ms) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(MoreSettingMapper.class);
            ms.setUpdateTime(new Date());
            MoreSetting moreSetting = mapper.selectById(ms.getId());
            boolean result = false;
            if (moreSetting == null) {
                result = (mapper.insert(ms)==1);
            }else {
                result = (mapper.updateById(ms)==1);
            }
//            boolean result = this.saveOrUpdate(ms);

            if (result) {//刷新缓存
                flushRedis(ms,2);
            }
            return result;
        } finally {
            endOfSession(session);
        }
    }
//
    @Override
    public MoreSetting getSetting(String imei){
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(MoreSettingMapper.class);
            MoreSetting ms = mapper.selectOne(new QueryWrapper<MoreSetting>().eq("imei", imei));
            if(null == ms) {
                ms = new MoreSetting();
                ms.setCreateTime(new Date());
                ms.setCardNum(imei);
                ms.setId(UUIDUtil.uuid());
            }
            return ms;
        } finally {
            endOfSession(session);
        }
    }
//
//    @Override
//    public List<DeviceMode> getDeviceModeByStudentCardId(String studentCardId) {
//        List<DeviceMode> deviceModeList = deviceModeMapper.getDeviceModeByStudentCardId(studentCardId);
//        return deviceModeList;
//    }
//
//    @Override
//    public List<DeviceMode> getDeviceModeBySchoolId(String SchoolId) {
//        return deviceModeMapper.getDeviceModeBySchoolId(SchoolId);
//    }
//
//    @Override
//    public String updateC40Status(String imei, Integer type, Integer status) {
//        MoreSetting setting = this.getSetting(imei);
//        Integer statuss = 2;
//        if(status == 1) {
//            statuss = 3;
//        }
//        JSONObject jo = new JSONObject();
//        jo.put("imei", imei);
//        jo.put("APPID", type);
//        jo.put("APPEN", status);
//        jo.put("type", "APPMAN");
//        jo.put("taskId", UUID.randomUUID().toString());
//        switch(type){
//            //消息发送
//            case 2:
//                Integer messageSendStatus = setting.getMessageSendStatus();
//                if(messageSendStatus == 2 || messageSendStatus == 3) {
//                    return ApiResultGenerator.errorResult(ApiResult.PARAM_ERROR_CODE, "指令正在执行中，请稍后再试！");
//                }
//                setting.setMessageSendStatus(status);
//                //闹钟设置下发设备 待开启
//                kafkaClient.c40ApplicationSend(jo.toJSONString());
//                break;
//            //2：闹钟设置
//            case 1:
//                Integer clockStatus = setting.getAlarmClockStatus();
//                if(clockStatus == 2 || clockStatus == 3) {
//                    return ApiResultGenerator.errorResult(ApiResult.PARAM_ERROR_CODE, "指令正在执行中，请稍后再试！");
//                }
//                setting.setAlarmClockStatus(statuss);
//                //闹钟设置下发设备 待开启
//                kafkaClient.c40ApplicationSend(jo.toJSONString());
//                break;
//            //3：短信代收
//            case 3:
//                Integer smsCollectionStatus = setting.getSmsCollectionStatus();
//                if(smsCollectionStatus == 2 || smsCollectionStatus == 3) {
//                    return ApiResultGenerator.errorResult(ApiResult.PARAM_ERROR_CODE, "指令正在执行中，请稍后再试！");
//                }
//                setting.setSmsCollectionStatus(statuss);
//                //短信代收下发设备 待开启
//                kafkaClient.c40ApplicationSend(jo.toJSONString());
//                break;
//            //4：通话记录
//            case 4:
//                Integer callRecordStatus = setting.getCallRecordStatus();
//                if(callRecordStatus == 2 || callRecordStatus == 3) {
//                    return ApiResultGenerator.errorResult(ApiResult.PARAM_ERROR_CODE, "指令正在执行中，请稍后再试！");
//                }
//                setting.setCallRecordStatus(statuss);
//                //通话记录下发设备 待开启
//                kafkaClient.c40ApplicationSend(jo.toJSONString());
//                break;
//            default:
//                break;
//        }
//        boolean b = this.saveOrUpdateSetting(setting);
//        if(b) {
//            return ApiResultGenerator.successResult(statuss);
//        }
//        return ApiResultGenerator.sysErrorResult();
//    }
//
//    @Override
//    public Integer remoteRestart(String imei) {
//        MoreSetting setting = this.getSetting(imei);
//        Integer remoteRestart = setting.getRemoteRestart();
//        if(remoteRestart == null) {
//            remoteRestart = 0;
//        }
//        setting.setRemoteRestart(remoteRestart + 1);
//        boolean b = this.saveOrUpdateSetting(setting);
//        logger.info("远程重启==>imei：{}，flag：{}", imei, b);
//        JSONObject jo = new JSONObject();
//        jo.put("imei", imei);
//        jo.put("type", "RESET");
//        //远程重启命令下发 代开启
//        kafkaClient.c40ApplicationSend(jo.toJSONString());
//        if(b) {
//            return 1;
//        }
//        return 0;
//    }
//
    @Override
    public void updateMoreSetting(C40DataVO c40DataVO) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(MoreSettingMapper.class);
            String imei = c40DataVO.getImei();
            String status = c40DataVO.getStatus();
            logger.info("应用程序回调，数据:{}", c40DataVO);
            if(StringUtils.isEmpty(imei)) {
                return;
            }
            MoreSetting ms = getSetting(imei);
            boolean flag = false;
            if("1".equals(status)) {
                if(ms.getAlarmClockStatus() == 2) {
                    ms.setAlarmClockStatus(0);
                    flag = true;
                    clockSettingsService.updateClockStatus(imei);
                } else if(ms.getAlarmClockStatus() == 3) {
                    ms.setAlarmClockStatus(1);
                    flag = true;
                }

                if(ms.getMessageSendStatus() == 2) {
                    ms.setMessageSendStatus(0);
                    flag = true;
                } else if (ms.getMessageSendStatus() == 3) {
                    ms.setMessageSendStatus(1);
                    flag = true;
                }

                if(ms.getSmsCollectionStatus() == 2) {
                    ms.setSmsCollectionStatus(0);
                    flag = true;
                } else if(ms.getSmsCollectionStatus() == 3) {
                    ms.setSmsCollectionStatus(1);
                    flag = true;
                }

                if(ms.getCallRecordStatus() == 2) {
                    ms.setCallRecordStatus(0);
                    flag = true;
                } else if(ms.getCallRecordStatus() == 3) {
                    ms.setCallRecordStatus(1);
                    flag = true;
                }
            } else {
                if(ms.getAlarmClockStatus() == 2) {
                    ms.setAlarmClockStatus(1);
                    flag = true;
                } else if(ms.getAlarmClockStatus() == 3) {
                    ms.setAlarmClockStatus(0);
                    flag = true;
                }

                if(ms.getMessageSendStatus() == 2) {
                    ms.setMessageSendStatus(1);
                    flag = true;
                } else if (ms.getMessageSendStatus() == 3) {
                    ms.setMessageSendStatus(0);
                    flag = true;
                }

                if(ms.getSmsCollectionStatus() == 2) {
                    ms.setSmsCollectionStatus(1);
                    flag = true;
                } else if(ms.getSmsCollectionStatus() == 3) {
                    ms.setSmsCollectionStatus(0);
                    flag = true;
                }

                if(ms.getCallRecordStatus() == 2) {
                    ms.setCallRecordStatus(1);
                    flag = true;
                } else if(ms.getCallRecordStatus() == 3) {
                    ms.setCallRecordStatus(0);
                    flag = true;
                }
            }
            if(flag) {
                ms.setUpdateTime(new Date());
                mapper.updateById(ms);
            }
        } finally {
            endOfSession(session);
        }
    }
//
//    @Override
//    public List<UpdateModeDto> selectUpdateImei(String schoolId) {
//        return mapper.selectUpdateImei(schoolId);
//    }
//
//    @Override
//    public StudentGuardSettingDto getGuardSetting(String imei) {
//
//        return trajectoryClient.getGuardSetting(imei);
//    }
//
//    @Override
//    public boolean saveGoSchoolGuard(StudentGuardSettingDto studentGuardSettingDto, String imei) {
//        StudentGuardSetting studentGuardSetting = new StudentGuardSetting();
//        BeanUtils.copyProperties(studentGuardSettingDto,studentGuardSetting);
//        return trajectoryClient.saveGoSchoolGuard(studentGuardSetting,imei);
//    }
//
//
//    @Override
//    public boolean openGoSchoolGuard(String imei,String cardType) {
//
//        try {
//            StudentGuardSettingDto studentGuardSetting = getGuardSetting(imei);
//
//            LocationMode locationMode = new LocationMode();
//            MoreSetting setting = getSetting(imei);
//            if (StringUtils.isNotBlank(setting.getModeType())) {
//                DeviceMode mode = deviceModeService.getById(setting.getModeType());
//                if (mode!=null) {
//                    locationMode.setGpsRepInterval(mode.getGpsRepInterval());
//                    locationMode.setPositionFirst(mode.getPositionFirst());
//                }else {
//                    locationMode.setGpsRepInterval(0);
//                    locationMode.setGpsRepInterval(1800);
//                }
//            }
//
//            GuardTimeUtil guardTimeUtil = new GuardTimeUtil();
//            String moArriveSchoolCron ="";
//            String moLateSchoolCron ="";
//            String moArriveHomeCron ="";
//            String afArriveSchoolCron ="";
//            String afLateSchoolCron ="";
//            String afLEaveSchoolCron ="";
//            String afArriveHomeCron ="";
//            List<GuardTimeInfo> tempList = new ArrayList<>();
//            if (studentGuardSetting!=null) {
//                if (studentGuardSetting.getMoArriveSchoolTime()!=null && studentGuardSetting.getMoArriveSchoolTime()!="") {
//                    String[] range = studentGuardSetting.getMoArriveSchoolRange().split("-");
//                    moArriveSchoolCron = guardTimeUtil.timeToCronUtil(range[0], studentGuardSetting.getRepeatDay());
//                    moLateSchoolCron = guardTimeUtil.timeToCronUtil(studentGuardSetting.getMoArriveSchoolTime(), studentGuardSetting.getRepeatDay());
//                    //上午未离家定时判断
//                    tempList.add(new GuardTimeInfo(imei,moArriveSchoolCron));
//                    //上午迟到定时判断
//                    tempList.add(new GuardTimeInfo(imei,moLateSchoolCron));
//                }
//                if (studentGuardSetting.getMoLeaveSchoolTime()!=null && studentGuardSetting.getMoLeaveSchoolTime()!="") {
//                    String[] range = studentGuardSetting.getMoLeaveSchoolRange().split("-");
//                    moArriveHomeCron = guardTimeUtil.timeToCronUtil(range[1], studentGuardSetting.getRepeatDay());
//                    //上午未到家定时判断
//                    tempList.add(new GuardTimeInfo(imei,moArriveHomeCron));
//                }
//                if (studentGuardSetting.getAfArriveSchoolTime()!=null && studentGuardSetting.getAfArriveSchoolTime()!="") {
//                    String[] range = studentGuardSetting.getAfArriveSchoolRange().split("-");
//                    afArriveSchoolCron = guardTimeUtil.timeToCronUtil(range[0], studentGuardSetting.getRepeatDay());
//                    afLateSchoolCron = guardTimeUtil.timeToCronUtil(studentGuardSetting.getAfArriveSchoolTime(), studentGuardSetting.getRepeatDay());
//                    //下午未离家定时判断
//                    tempList.add(new GuardTimeInfo(imei,afArriveSchoolCron));
//                    //下午迟到定时判断
//                    tempList.add(new GuardTimeInfo(imei,afLateSchoolCron));
//                }
//                if (studentGuardSetting.getAfLeaveSchoolTime()!=null && studentGuardSetting.getAfLeaveSchoolTime()!="") {
//                    String[] range = studentGuardSetting.getAfLeaveSchoolRange().split("-");
//                    afArriveHomeCron = guardTimeUtil.timeToCronUtil(range[1], studentGuardSetting.getRepeatDay());
//                    //下午未到家定时判断
//                    tempList.add(new GuardTimeInfo(imei,afArriveHomeCron));
//                }
//            }
//
//            List<GuardTimeInfo> timeInfoList = (ArrayList<GuardTimeInfo>) redisService.get("guardTimeInfoList", ArrayList.class);
//            if (timeInfoList!=null) {
//                timeInfoList.addAll(tempList);
//            }else {
//                timeInfoList = tempList;
//            }
//            studentGuardSetting.setIsOpen(1);
//            saveGoSchoolGuard(studentGuardSetting, imei);
//            redisService.set("guardTimeInfoList",timeInfoList);
//            kafkaClient.sendMod(imei,cardType,locationMode);
//            logger.info("----------m4模式指令下发成功");
//
//            return true;
//        } catch (BeansException e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
//
//    @Override
//    public boolean closeGoSchoolGuard(String imei,String cardType) {
//
//        if (trajectoryClient.closeGoSchoolGuard(imei)) {
//
//            String guardTimeInfoList = (String) redisService.get("guardTimeInfoList", String.class);
//            List<GuardTimeInfo> timeInfoList = JSONObject.parseArray(guardTimeInfoList, GuardTimeInfo.class);
//            Iterator<GuardTimeInfo> iterator = timeInfoList.iterator();
//            while (iterator.hasNext()) {
//                GuardTimeInfo timeInfo = iterator.next();
//                if (timeInfo.getImei().equals(imei)) {
//                    iterator.remove();
//                }
//            }
//            redisService.set("guardTimeInfoList",timeInfoList);
//            LocationMode locationMode = new LocationMode();
//            MoreSetting setting = getSetting(imei);
//            if (StringUtils.isNotBlank(setting.getModeType())) {
//                DeviceMode mode = deviceModeService.getById(setting.getModeType());
//                if (mode!=null) {
//                    locationMode.setGpsRepInterval(mode.getGpsRepInterval());
//                    locationMode.setPositionFirst(mode.getPositionFirst());
//                }else {
//                    locationMode.setGpsRepInterval(0);
//                    locationMode.setGpsRepInterval(1800);
//                }
//            }
//            kafkaClient.sendMod(imei,cardType,locationMode);
//            return true;
//        }
//        return false;
//    }
//
//
    @Override
    public boolean deleteSettings(String imei) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(MoreSettingMapper.class);
            boolean result = (mapper.delete(new QueryWrapper<MoreSetting>().eq("imei", imei))==1);
            if (result) {
                MoreSetting ms = new MoreSetting();
                ms.setCardNum(imei);
                flushRedis(ms, 1);
            }
            return result;
        } finally {
            endOfSession(session);
        }
    }
//
//    @Override
//    public LocationMode getLocationFreq(String imei) {
//        return mapper.getLocationFreq(imei);
//    }
//
    /**
     * @param ms
     * @param type 1删除，2save
     */
    private void flushRedis(MoreSetting ms, int type) {
        List<MoreSetting> settings = redisService.getList(CommonResource.CARD_SETTING_LIST, MoreSetting.class);
        if (settings==null || settings.isEmpty()) {
            settings = new ArrayList<>();
        }
        boolean needAdd = true;
        for (int i = settings.size() - 1; i >= 0; i--) {
            MoreSetting setting = settings.get(i);
            if (null == setting) {
                settings.remove(i);
                continue;
            }
            if (setting.getCardNum().equals(ms.getCardNum())) {
                settings.remove(i);
                if (type == 2) {
                    settings.add(i, ms);
                    needAdd = false;
                }
                break;
            }
        }
        if (needAdd) {
            settings.add(ms);
        }
        redisService.set(CommonResource.CARD_SETTING_LIST, settings);
        redisService.remove(CommonResource.PRE_COMMAND_MODE + ms.getCardNum());//切换模式，清除缓存
    }

    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();
        }
    }
}
